<?php
require_once("../config.php");
page_constant("reg");

if(!empty($_GET['ic']))
{
	$invitefrom=$dsql->getone("select id from user where invitecode = '".$_POST['invitecode']."'");
	if (!empty($invitefrom))
		$tpl->display("reg.tpl");
}
elseif(isset($_POST['username']))
{
	$uid=new UID();
	$data=array('id'=>'',
	'username'=>$_POST['username'],
	'password'=>md5($_POST['userpassword']),
	'email'=>$_POST['email'],
	'phone'=>$_POST['phone'],
	'realname'=>$_POST['realname'],
	'invitecode'=>generate_invitecode(),
	'invitefrom'=>$invitefrom,
	'time'=>'now()');
	$uid->insert("user",$data);
	
}

function generate_invitecode()
{
	global $dsql;
	$length=12;
	$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $password = '';
    for ( $i = 0; $i < $length; $i++ )
    {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符；
        // 第二种是取字符数组 $chars 的任意元素
         $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        //$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
	$rs=$dsql->getone("select id from user where invitecode = '$password'");
	if(!$rs)										
		return $password;
	else
		generate_invitecode();
}



?>